import { BasicColumn, FormSchema } from '/@/components/Table';
import { render } from '/@/utils/common/renderUtils';
import { rules} from '/@/utils/helper/validator';
import { DescItem } from '/@/components/Description/index';
import { ChannelTypeEnum } from './model';
import { JVxeTypes, JVxeColumn } from '/@/components/jeecg/JVxeTable/types'
import { useUserStore } from '/@/store/modules/user'
import { listDataGroupTree } from '/@/api/infra/dataGroup';
import { DataGroupBizType } from "/@/components/BizDataGroup/index"
import { PageEnum } from '/@/enums/pageEnum';
import { ConfigKey } from '/@/views/system/config/config.model';
import { TenantType } from '/@/enums/bizEnum';
const userStore = useUserStore()
import { isManageOrPlatform }from '/@/utils/tenant'

export const columns: BasicColumn[] = [
  {
    title: '消息名称',
    dataIndex: 'name',
    width: 200,
  },  
  {
    title: '消息编码',
    dataIndex: 'code',
    width: 200,
  },
  {
    title: '状态',
    dataIndex: 'status',
    width: 80,
    customRender: ({ text }) => {
      const color = text ? 'green' : 'red';
      return render.renderTag(text ? '启用' : '禁用', color)
    }
  },
  {
    title: '渠道类型',
    dataIndex: 'channelList',
    align: 'left',
    minWidth: 300,
    slot: 'channelList'
  }
];


export const searchFormSchema: FormSchema[] = [
  {
    field: 'name',
    label: '名称',
    component: 'Input'
  },
  {
    field: 'receiverType',
    label: '接收者类型',
    component: 'JDictSelectTag',
    componentProps: {
      allowClear: true,
      dictCode:'msg_receiver_type',
    },
  },
  {
    field: 'groupId',
    label: '消息组',
    component: 'ApiSelect',
    componentProps: ({ formActionType }) => {
      return {
        api: listDataGroupTree,
        params: {
          bizType: DataGroupBizType.MSG_PUSH_TASK,
        },
        allowClear: true,
        optionFilterProp: "label",
        showSearch: true,
        labelField: 'name',
        valueField: 'id',
        dropdownStyle: { maxHeight: '16vh', overflow: 'auto' },
      }
    }
  },
  {
    label: '渠道类型',
    field: 'channelType',
    component: 'JDictSelectTag',
    defaultValue: [],
    componentProps: {
      dictCode: 'message_channel_type',
      showSearch: true,
      allowClear: true,
    }
  },
  {
    field: 'status',
    label: '状态',
    component: 'JDictSelectTag',
    componentProps: {
      dictCode: 'data_use_status',
    },
  },
];


export const selectSubNoticeSearchForm: FormSchema[] = [
  {
    field: 'name',
    label: '名称',
    component: 'Input'
  }
]

export const formSchema: FormSchema[] = [
  {
    field: 'id',
    label: 'id',
    component: 'Input',
    show: false,
    required: false
  },
  // {
  //   field: 'name',
  //   label: '名称',
  //   component: 'Input',
  //   required: true
  // },
  {
    field: 'isShowSelectTask',
    label: 'isShowSelectTask',
    component: 'RadioGroup',
    show: false,
    required: false
  },
  {
    field: 'name',
    label: '消息名称',
    dynamicDisabled: ({values}) => {
      return !isManageOrPlatform(userStore)
    },
    component: 'Input',
    required: true
  },
  {
    field: 'code',
    label: '消息编码',
    dynamicDisabled: ({values}) => {
      return !isManageOrPlatform(userStore)
    },
    component: 'Input',
    required: true
  },

  {
    field: 'receiverType',
    label: '接收者类型',
    defaultValue: '1',
    // helpMessage: '如果是会员通知, 则在代码中可以为任意用户推送',
    component: 'JDictSelectTag',
    componentProps: {
      type: "radio",
      dictCode:'msg_receiver_type',
    },
    dynamicDisabled: ({values}) => {
      return !isManageOrPlatform(userStore)
    },
  },

  {
    label: '消息功能组',
    field: 'groupId',
    component: 'ApiSelect',
    dynamicDisabled: ({values}) => {
      return !isManageOrPlatform(userStore)
    },
    componentProps: {
      api: listDataGroupTree,
      params: {
        bizType: DataGroupBizType.MSG_PUSH_TASK,
      },
      optionFilterProp: "label",
      showSearch: true,
      labelField: 'name',
      valueField: 'id',
      dropdownStyle: { maxHeight: '16vh', overflow: 'auto' },
    }
  },

  {
    label: '渠道类型',
    field: 'channelTypes',
    required: true,
    component: 'JDictSelectTag',
    componentProps: {
      mode: 'multiple',
      type: 'list',
      dictCode: 'message_channel_type',
      showSearch: true
    }
  },

  {
    field: 'status',
    label: '状态',
    defaultValue: true,
    required: true,
    component: 'RadioGroup',
    componentProps: {
      options: [
        { label: '启用', value: true },
        { label: '禁用', value: false },
      ],
    },
  },

  {
    label: '备注',
    field: 'remark',
    dynamicDisabled: ({values}) => {
      return !isManageOrPlatform(userStore)
    },
    component: 'InputTextArea',
    componentProps: {
      placeholder: '请输入备注',
      rows: 5,
    },
  }
];


export const formDetailSchema: DescItem[] = [
  {
    field: 'name',
    label: '消息名称',
  },
  {
    field: 'channelTypeNames', 
    label: '渠道类型',
    render: (curVal, data) => {
      const {channelId} = data
      return render.renderTag(curVal, 'cyan')
    },
  },

  {
    field: 'status',
    label: '状态',
    render: (curVal, data) => {
      const color = curVal ? 'green' : 'red';
      return render.renderTag(curVal ? '启用' : '禁用', color)
    }
  },
  {
    field: 'createdAt',
    label: '创建时间'
  },
  {
    field: 'createdBy',
    label: '创建者'
  },
  {
    field: 'updatedAt',
    label: '更新时间'
  },
  {
    field: 'updatedBy',
    label: '更新者'
  },
  {
    label: '备注',
    labelMinWidth: 40,
    field: 'remark',
  }
];



export const channelColumns: BasicColumn[] = [
  {
    title: '渠道类型',
    dataIndex: 'channelTypeName',
    width: 100,
  },
  {
    title: '渠道名称',
    dataIndex: 'channelName',
    width: 160,
  }
];

export const channelConfigSchema: FormSchema[] = [
  {
    field: 'status',
    label: '状态',
    defaultValue: '0',
    helpMessage: '被禁用将不会推送消息',
    required: true,
    component: 'JDictSelectTag',
    componentProps: {
      type: 'radio',
      dictCode: 'data_use_status',
    },
  },
  {
    field: 'title',
    label: '标题',
    required: false,
    ifShow: ({ values }) => {
      return values.channelType === ChannelTypeEnum.EMAIL || 
             values.channelType === ChannelTypeEnum.PC
    },
    component: 'Input',
  },
  {
    field: 'templateId',
    label: '模板ID',
    helpMessage: '第三方模板id, eg: 微信小程序/微信公众后消息模板id',
    ifShow: ({ values }) => {
      return values.channelType !== ChannelTypeEnum.EMAIL && values.channelType !== ChannelTypeEnum.PC
    },
    dynamicDisabled: ({values}) => {
      return userStore.getSelectTenant.tenantType === TenantType.SUB
    },
    required: ({ values }) => values.channelType !== ChannelTypeEnum.EMAIL && values.channelType !== ChannelTypeEnum.PC,
    component: 'Input',
  },
  {
    // 小程序或者PC推送才拥有
    field: 'pageUrl',
    label: '页面地址',
    helpMessage: [
      '1、点击推送的消息将会跳转到该地址',
      '2、支持占位符 ${h5Url} 来表示在系统中配置的H5域名',
      '3、如果指定了小程序appId, 则填写小程序地址',
    ],
    dynamicDisabled: ({values}) => {
      return !isManageOrPlatform(userStore)
    },
    required: false,
    ifShow: ({ values }) => {
      return values.channelType === ChannelTypeEnum.PC || 
             values.channelType === ChannelTypeEnum.WX_MA || 
             values.channelType === ChannelTypeEnum.WX_MP
    },
    component: 'Input',
  },
  {
    field: 'channelType',
    label: 'channelType',
    show: false,
    component: 'Input',
  },
  {
    // PC推送或者APP推送才拥有
    field: 'mediaId',
    label: '媒体文件',
    helpMessage: '推送的时候会以链接形式推送给目标, 需要从素材库中选择',
    required: false,
    ifShow: ({ values }) => {
      return values.channelType === ChannelTypeEnum.PC
    },
    component: 'MySelectMaterial',
  },
  {
    // 是否跳转到小程序
    field: 'isJumpMa',
    label: '关联小程序',
    helpMessage: [
      '1、点击推送的消息且指定了页面地址则跳转到小程序',
      `2、<a target='_blank' href='${PageEnum.CONFIG_CENTER}?configKey=${ConfigKey.WX_MA.code}'>去配置</a>, 也可以指定其他小程序appId`
    ],
    required: false,
    ifShow: ({ values }) => {
      return values.channelType === ChannelTypeEnum.WX_MP
    },
    component: 'RadioGroup',
    componentProps: {
      options: [
        {
          label: '是',
          value: true,
        },
        {
          label: '否',
          value: false,
        },
      ],
    },
  },
  {
    // 跳转到的小程序appId
    field: 'maAppId',
    label: '小程序appId',
    helpMessage: [
      '1、点击推送的消息且指定了页面地址则跳转到小程序',
      '2、指定后, 优先使用指定的小程序appId, 如果没有指定, 则使用系统中配置的小程序id',
    ],
    required: false,
    ifShow: ({ values }) => {
      return values.channelType === ChannelTypeEnum.WX_MP
    },
    component: 'Input'
  },
  {
    field: 'content',
    label: '模板内容',
    helpMessage: '仅作为在推送历史中消息查看',
    slot: 'contentEdit',
    component: 'Input',
  }
];


/**
 * 消息接收人
 */
export const receiverColumns : BasicColumn[] = [
  {
    title: '姓名',
    dataIndex: 'name',
    width: 100,
  },
  {
    title: '手机号',
    dataIndex: 'phone',
    slots: { customRender: 'phone' },
    width: 120,
  },
  {
    title: '邮箱',
    dataIndex: 'email',
    slots: { customRender: 'email' },
    width: 150,
  },
  {
    title: '备注',
    dataIndex: 'remark',
    width: 100,
  }
];

